package com.dk.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dk.data.Result;
import com.dk.em.ConstantEnum;
import com.dk.entity.TContent;
import com.dk.entity.TContentCategory;
import com.dk.entity.TContentOrder;
import com.dk.entity.TContentPrice;
import com.dk.service.ITContentCategoryService;
import com.dk.service.ITContentOrderService;
import com.dk.service.ITContentPriceService;
import com.dk.utile.IdWorker;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;

@RestController
@RequestMapping("contentController")
@Api(value = "广告投放")
public class ContentController {

    @Autowired
    private ITContentCategoryService itContentCategoryService;

    @Autowired
    private ITContentOrderService itContentOrderService;

    @Autowired
    private ITContentPriceService priceService;

    @ApiOperation(value = "广告价格查询接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "categoryId",value = "广告类型名称",required = true),
            @ApiImplicitParam(name = "contentSort",value = "广告类型排序",required = true),
            @ApiImplicitParam(name = "startTime",value = "广告开始时间",required = true),
            @ApiImplicitParam(name = "endTime",value = "广告结束时间",required = true)
    })
    @GetMapping("/findInfo")
    public Result findInfo(Integer categoryId, Integer contentSort,TContent content){
        System.out.println(content.getStartTime());
        TContentPrice price = new TContentPrice();
        QueryWrapper Wrapper = new QueryWrapper<TContentPrice>();
        Wrapper.eq("category_id",categoryId);
        Wrapper.eq("content_sort",contentSort);
        TContentPrice contentPrice = priceService.getOne(Wrapper);

        long start = content.getStartTime().getTime();
        long end = content.getEndTime().getTime();
        long time = Math.abs((end - start) / 24 * 3600 * 1000);
        int day = (int)time;

        BigDecimal p = new BigDecimal(day);
        BigDecimal decimal = p.multiply(contentPrice.getPrice());
        price.setPrice(decimal);
        return Result.bulid(ConstantEnum.SUCCESS).setResult(contentPrice);
    }

    @GetMapping
    @ApiOperation(value="获取所有广告类型")
    public Result getContentCategory(){
        List<TContentCategory> tContentCategoryList = itContentCategoryService.selecCategorytList();
        return Result.bulid(ConstantEnum.SUCCESS).setResult(tContentCategoryList);
    }

    @ApiOperation("生成广告订单")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "tContentOrder",value = "广告订单实体类",required = true,paramType = "body"),
            @ApiImplicitParam(name = "categoryId",value = "广告类型",required = true,paramType = "query"),
            @ApiImplicitParam(name = "title",value = "广告标题",required = true,paramType = "query"),
            @ApiImplicitParam(name = "pic",value = "广告图片",required = true,paramType = "query"),
            @ApiImplicitParam(name = "sort",value = "广告排序",required = true,paramType = "query"),
            @ApiImplicitParam(name = "startTime",value = "开始时间",required = true,paramType = "query"),
            @ApiImplicitParam(name = "endTime",value = "结束时间",required = true,paramType = "query"),
            @ApiImplicitParam(name = "chargingMode",value = "收费模式",required = true,paramType = "query")
    })
    @PostMapping(value = "/insertContentOrder")
    public Result insertContentOrder(@RequestBody TContentOrder tContentOrder,TContent tContent){
        IdWorker idWorker = new IdWorker(0,0);
        //创建订单号
        Long orderid = idWorker.nextId();
        tContentOrder.setOrderId(orderid.toString());
        tContentOrder.setStatus(0);

        String categoryId = tContent.getCategoryId();
        tContent.setUrl(categoryId + ".html"); //广告的url地址
        tContent.setDeleted(0);
        tContent.setVersion(0);
        tContent.setOrderId(orderid.toString());  //订单号

        //增加订单
        Boolean b = itContentOrderService.insertContentOrder(tContentOrder,tContent);
        if(b){
            return Result.bulid(ConstantEnum.SUCCESS);
        }
        return Result.bulid(ConstantEnum.ERROR);
    }
}
